package spring.config;

import org.springframework.context.annotation.*;
import spring.aware.AwareService;
import spring.bean.Person;
import spring.bean.PrototypeBean;
import spring.bean.Student;
import spring.bean.Teacher;

@Configuration //表明这是一个配置类
@ComponentScan(basePackages={"spring.**"})  //配置要扫描bean的包
@EnableAspectJAutoProxy //启动aop自动代理功能
public class SpringConfig {

    /**
     * 通过java方式配置bean，而不是通过注解方式配置
     * @return
     */
    @Bean(initMethod = "init", destroyMethod = "destroy") //单例在spring容器销毁时执行destroy方法，prototype则不会
    @Scope("prototype")
    public Student createStudent() {
        return new Student();
    }

    @Bean
    public Teacher createTeacher() {
        return new Teacher();
    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        Person bean = context.getBean("person", Person.class);
        bean.say("人类");

        context.getBean(Teacher.class).say("教师");

        context.getBean(Student.class).say("学生");
        context.getBean(Student.class).say("学生");

        context.getBean(PrototypeBean.class);
        context.getBean(PrototypeBean.class);

        context.getBean(AwareService.class).outputResult();
        context.close();
    }
}
